﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ManagerCategory.ascx.cs" Inherits="UserControl_ManagerCategory" %>
<%@ Import Namespace="DacSanMienTayOBJ" %>

<%@ Import Namespace="System.Collections.Generic" %>
<script type="text/javascript" src="Scripts/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="<%=Utility.modHost %>/Scripts/jquery.simplemodal.js"></script>

<style>
#simplemodal-overlay {background-color:#000;}
#simplemodal-container {height:378px; width:619px; color:#bbb; background:url(../Images/bg_login.png) no-repeat center; padding:12px; position:relative;}
#simplemodal-container .simplemodal-data {position:relative;}
#simplemodal-container a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:0px; right:0px; cursor:pointer;}

#simplemodal_container_uploadimage {height:490px; width:700px; color:#bbb;background-color:White;border:5px;border-style:solid;border-color:#ce5c00; position:relative;margin-top:50px}
#simplemodal_container_uploadimage .simplemodal-data {position:relative;}
#simplemodal_container_uploadimage a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:-16px; right:-16px; cursor:pointer;}

</style>

<div style=" margin-left: 70px; margin-top: 10px;">


		<select style="position:relative; left:-70px" name="cbSection" id="cbSection" onchange="FilterFollowSection()" >
								       <option value="-1" selected="selected">Tất cả</option>
				                     <option value="1">Category lớn</option>
				                      <option value="0">Category giữa</option>
				                      <option value="2">Category nhỏ </option>
								</select>
	
								<div style="height:20px"></div>
								
											<input type="button" style="position:relative; left:-70px" value="Tạo Category mới" onclick="ShowFormCategory()" /><br /><br />
				<div id="div_Category" style="display:none">
  	
       
        <div id="divInfo" style="height:340px">
        <div style="width:600px; height:20px; padding:0px 0px 0px 150px; font-size:16px; font-weight:bold; color:#4D4D4D ">
        <input type="radio" id="rdCateOne" name="rdType" onclick="ShowType(1)" checked="checked" />Category lớn
        <input type="radio" id="rdCateTwo" name="rdType"  onclick="ShowType(2)" />Category Giữa
        <input type="radio" id="rdCateThree" name="rdType"  onclick="ShowType(3)" />Category nhỏ
        
        
        </div>     
      <div style="height:5px"></div>    
        
        
          <input type="hidden" id="hdCateID" name="hdCateID"  />
      <div id="divCateNameCate" style="margin-left:0px; line-height:30px; ">
       <span style="color:black"> Tên category : </span> <input type="text" id="txtCategoryName" name="txtCategoryName" onfocus="FocusField(1)" style="width:300px; margin-left:22px" />
        <label id="lbErrorCategoryName"  style=" position:relative; margin-left:0px; color:red" > </label>
       </div>
       <div id="divCateMax" style="display:none">
          <span style="color:black"> Parent Category : </span> 
       <select id="cbParentCate" name="cbParentCate" onclick="FocusField(2)" >
       
       <%
                    try
                    {

                        List<Category> listParentCate = CategoryBusiness.ListAllCategoryFollowSection(1,ConnectionType.AdminConnection);
                        if (listParentCate.Count > 0)
                        {
                            foreach (Category cate in listParentCate)
                            {  
                               
                                %>
                                <option value="<%= cate.CategoryID %>"><%=cate.CategoryName %></option>
                                <%
                            }
                        }
                    }
                    catch(Exception e)
                    {
                        
                    }
                 %>
       </select>
       
        <label id="lbErrorParrentCate"  style=" position:relative; margin-left:0px; color:red" > </label>
         
       </div>
       
        <div id="divCateMiddle" style="display:none">
          <span style="color:black"> Middle Category : </span> 
       <select id="cbMiddleCate" name="cbMiddleCate" onclick="FocusField(3)" >
       
       <%
                    try
                    {

                        List<Category> listParentCate = CategoryBusiness.ListAllCategoryFollowSection(0, ConnectionType.AdminConnection);
                        if (listParentCate.Count > 0)
                        {
                            foreach (Category cate in listParentCate)
                            {  
                               
                                %>
                                <option value="<%= cate.CategoryID %>"><%=cate.CategoryName %></option>
                                <%
                            }
                        }
                    }
                    catch(Exception e)
                    {
                        
                    }
                 %>
       </select>
        <label id="lbErrorMiddleCate"  style=" position:relative; margin-left:0px; color:red" > </label>
         
       </div>
       
     <%-- <div style="margin-left:0px; line-height:30px; color:Silver;"> IsMain : 
         
        <input type="checkbox" id="cbIsMain" name="cbIsMain"  />
        
       </div>--%>
      
   
      <div style=""></div>
      <div style=" width:450px; height:20px; margin:1px 0px 5px 0px; font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#000000" >Meta Keyword </div>
      <div style="margin:0px 0px 10px 0px" >
     
        <textarea id="txtKeyWord" name="txtKeyWord"  style="width:750px;height:75px" cols="5" rows="3"></textarea>
       
      </div>

             <div>
Logo

<asp:FileUpload ID="fileLogo" runat="server" />
<label id="lbErrorLogo" style="color:Red"></label>
<img id="imgCate" width="100px" height="100px" />

      </div>
      
      </div>
     


  <div>
 <div style="width:400px; left:-70px; position:relative">
<asp:Button ID="cmdAdd" Text="Thêm mới" runat="server" 
        OnClientClick=" return CheckValidate()" onclick="cmdAdd_Click" />
        </div>
<div style="position: relative; left:500px; top:-20px">
<asp:Button ID="cmdUpdate" Text="Cập nhật" runat="server" 
        OnClientClick=" return CheckValidateUpdate()" onclick="cmdUpdate_Click"  />
        </div>
        </div>
<input type="hidden" id="hdCateIDMain" name="hdCateIDMain" />
<input type="hidden" id="hdIsMain" name="hdIsMain" />
<input type="hidden" id="hdLogo" name="hdLogo" />
   <input type="hidden" id="hdTypeCate" name="hdTypeCate" />
	</div>
    
</div>


<div>
<span> Danh sách Category</span>

<table>
<tr>
<td style="width:200px">
Tên category
</td>
<td style="width:120px">
Hình ảnh
</td>
<td style="width:150px">
Tên Category lớn
</td>
<td style="width:150px">
Từ khó
</td>
<td style="width:100px">
Tình trạng
</td>
<td style="width:100px">
Chỉnh sữa
</td>

<td style="width:150px">
Khóa/mở khóa
</td>
</tr>

<tbody id="bodyCategory">
<%
    try
    {
        List<Category> list = CategoryBusiness.ListAllCategory(ConnectionType.AdminConnection);
        int vt = 0;
        foreach (Category cate in list)
        {
            string cateName = cate.CategoryName;
            string parentCateName =cate.ParentCategoryName;
            string cateImage = Utility.modHost + "/" + cate.CategoryImage;
            string status = "";
            if (cate.IsVisible == 1)
            {
                status = "Active";
            }
            else
            {
                status = "Inactive";
            }
            
          
            %>
            <tr id="trCategory<%=vt %>">
            <td><%=cateName%> </td>
            <td><img src="<%=cateImage %>" width="100px" height="100px" /></td>
            <td><%=parentCateName%></td>
            <td><%=cate.MetaKeyword %></td>
            <td><%=status %></td>

         <td>
         <span style="color:Blue; cursor:pointer" title="<%=cateName %>" id="<%=cate.MetaKeyword %>"  onclick="ShowInfoCategory('<%=cate.CategoryID %>','<%=cate.ParentCategoryID %>','<%=cateImage %>',this.title,this.id,'<%=cate.IsSection %>')">Chỉnh sữa</span>
         </td>
       <td>
        <%
            if (cate.IsVisible == 1)
            {
                 %>
                    <input type="button" title="<%=cate.CategoryID %>"  value="Khóa" onclick="LockAndUnlockCategory(this.title,'lock','<%=vt %>')" />
                <%
            }
            else
            {
             %>
                 <input type="button" title="<%=cate.CategoryID %>"  value="Mở khóa" onclick="LockAndUnlockCategory(this.title,'unlock','<%=vt %>')" />
                <%
            }
                     %>

                     
       </td>

            </tr>
            <%
            
                vt++;
        }
    }
    catch
    {

    }
    
     %>
</tbody>

</table>
</div>


<script type="text/javascript">

    function ShowFormCategory() {

        $('#div_Category').toggle("slow");
        $('#hdCateID').val('');
        $('#txtCategoryName').val('');
        $('#txtKeyWord').val('');

    }

    function ShowType(id) {
        if (id == '1') {
            $('#divCateMax').css("display", "none");
            $('#divCateMiddle').css("display", "none");

        }

        else
            if (id == '2') {
                $('#divCateMax').css("display", "inline");
                $('#divCateMiddle').css("display", "none");
            }

            else if (id == '3') {
                $('#divCateMax').css("display", "none");
                $('#divCateMiddle').css("display", "inline");
            }
    }

    function FocusField(value) {
        if (value == '1') {
            $('#lbErrorCategoryName').text('');
        }
        if (value == '2') {
            $('#lbErrorParrentCate').text('');
        }
        if (value == '3') {
            $('#lbMiddleCate').text('');
        }


    }

    function CheckValidate() {
        var nameCate = $('#txtCategoryName').val();
//        var isMain = 0;
//        if (document.getElementById('cbIsMain').checked == true) {
//            isMain = 1;
//            $('#hdIsMain').val(isMain);
//        }
//        else if (document.getElementById('cbIsMain').checked == false) {
//            isMain = 0;
//            $('#hdIsMain').val(isMain);
//        }
        var checkParent = 'no';
        var checkMiddle = 'no';
        var idParentCate = 0;
        var idMiddleCate = 0;
        var typeCate = 1;
        if (document.getElementById('rdCateTwo').checked == true) {
            idParentCate = $('#cbParentCate').val();
            checkParent = 'yes';
            typeCate = 0;
        }
       
        if (document.getElementById('rdCateThree').checked == true) {
            idMiddleCate = $('#cbMiddleCate').val();
            checkMiddle = 'yes';
            typeCate = 2;
        }
        $('#hdTypeCate').val(typeCate);
        var cateID = $('#hdCateID').val();
       
        if (cateID == '') {
            cateID = 0;
        }
        var check = 'false';

        if (nameCate == '') {
            check = 'true';
            $('#lbErrorCategoryName').text('Vui lòng điền tên Category');
        }

        if (checkParent == 'yes') {
            if (idParentCate == '0' || idParentCate == " ") {
                check = 'true';
                $('#lbErrorParrentCate').text('Vui lòng chọn parent category');
            }
        }

        if (checkMiddle == 'yes') {
            if (idMiddleCate == '0'||idMiddleCate==" ") {
                check = 'true';
                $('#lbErrorMiddleCate').text('Vui lòng chọn middle category');
            }
        }
        
        if (check == 'true') {

            return false;
        }
        else {

            return true;
        }
    }

    function ShowInfoCategory(cateID, parentCateID, cateImage, cateName, metakey, section) {
        $('#div_Category').css("display","inline");
        $('#hdCateID').val(cateID);
        if (section == 0) {

            $('#cbParentCate option[value=' + parentCateID + ']').attr("selected", true);
            document.getElementById('rdCateTwo').checked = true;
            $('#divCateMax').css("display", "inline");
            $('#divCateMiddle').css("display", "none");
            $('#hdTypeCate').val(0);
        }
        else if (section == 2) {
            $('#cbMiddleCate option[value=' + parentCateID + ']').attr("selected", true);
            document.getElementById('rdCateThree').checked = true;
            $('#divCateMax').css("display", "none");
            $('#divCateMiddle').css("display", "inline");
            $('#hdTypeCate').val(2);
        }
        else if(section==1){
            $('#divCateMax').css("display", "none");
            $('#divCateMiddle').css("display", "none");
            document.getElementById('rdCateOne').checked = true;
            $('#hdTypeCate').val(1);
        }
        $('#txtKeyWord').val(metakey);
        $('#txtCategoryName').val(cateName);
        $('#imgCate').attr("src", cateImage);

        $('body,html').animate({ scrollTop: 0 }, 300);
    }

    function LockAndUnlockCategory(cateID,type,vt) {
        var trID = '#trCategory' + vt;
        var load = '<img src="<%=Utility.modHost %>/Images/loading.gif" id="imgLoad" alt="" style="margin-left:auto;margin-right:auto;margin-top:53%;margin-left:135%;width:40px;height:40px" />';
        $(trID).html(load);

        $.post("<%=Utility.modHost %>/Proccess/ProccessLockAndUnlockCategory.aspx", { cateID: cateID, type: type, vt: vt }, function (data) {
            $('#imgLoad').attr("src", "");
            if (data == "lockfaile") {
                alert('Không khóa được category này. Vui lòng liên hệ admin');
            }
            else if (data == "unlockfaile") {
                alert('Không mở khóa được category này. Vui lòng liên hệ admin');
            }
            else {
                document.getElementById('trCategory' + vt).innerHTML = data;
            }
        });
    }

</script>